﻿

$(function () {
    $(".nb-canvas .am-icon-close").click(function () {
        $(".nb-sharecanvas").css("display", "none");
    });
    $('.search-reset').on('click', function () {
        $('.search-ipt').val('');
    });
    $('.search-cancel').on('click', function () {

    })
    $('.sstop').click(function () {
        var url = $(this).attr('data-info');
        window.location.href = url;
    });
    get_list(0);
})
        

        
function PopHide() {
    $("#mask").hide();
    $("#pop").hide();
}
function ProductPop(obj1) {
    var obj = eval('(' + unescape(obj1) + ')');
            
    var title = obj.title.replace(/<span class=H>/g, "").replace(/<\/span>/g, ""), zkPrice = obj.org_price, auctionId = obj.num_iid, Price = obj.price, couponAmount = obj.coupons_price, pictUrl = obj.pic_url
    var Price = zkPrice - couponAmount;
    $.ajax({
        type: "post",
        url: "/u/SearchKl.html",
        data: {
            id: obj.num_iid,
            t2: obj.t2,
            t1: obj.t1,
            t3: obj.t3,
            t4: obj.t4,
            Price: Price
        },
        success: function (dd) {
  
            dd = eval('(' + dd + ')');
            var html = "";
            html += ("<div class=\"pop_header\"><p>产品详情</p><span onclick='PopHide()'></span></div><div class=\"pop_center\"><div class=\"pop_detail_title clearfix\">");
            html += ("<img src=\"" + pictUrl + "\" />");
            html += ("<h6>" + title + "</h6></div><div class=\"pop_detail clearfix\">");
            if (obj.userType === 0)
                html += ("<span class=\"tao fl\"></span>");
            else
                html += ("<span class=\"tianmao fl\"></span>");

            html += ("<p class=\"fl\">淘宝</p></div><div class=\"pop_detail clearfix\"><p class=\"theme fl\">价格：</p>");
            html += ("<p class=\"fl xian\">￥" + Price + "</p>");
            html += ("<p class=\"fl yuan\">￥" + zkPrice + "</p></div><div class=\"pop_detail clearfix\">");

            html += ("<p class=\"theme fl\">优惠：</p><p class=\"fl youhui\">￥" + couponAmount + "</p></div>");
            html += ("<div class=\"pop_detail clearfix\"><p class=\"theme fl\">佣金：</p>");
            html += ("<p class=\"fl yongjin\">￥" + dd.ratePrice + "</p>");
            html += ("<p class=\"bili\">（收入比率<b>" + dd.rate + "%</b>）</p></div>");

            html += ("<div class=\"pop_detail clearfix\">");
            html += ("<a href=\"" + obj.url + "\" target=\"_blank\" class=\"fl j_btn red\">商品链接</a>");

            html += ("<a class=\"fl j_btn gray\" onclick='copy(this)' id='copyid' >复制文案</a>");
            html += ("<a class=\"fl j_btn waming\" style=\"display:block\" onclick='zhuanfa(" + auctionId + ")'  id='zhuanfa' >转发到群</a>");
            html += ("</div>");
            html += ("<div class=\"pop_detail clearfix\">");
         

            html += ("<textarea class='taodetail'>" + title +
                "\n ┄┄┄┄┄┄"+
                "\n 原　价：【"+zkPrice+"】元"+
         "\n券后【" + Price + "元】包邮秒杀"+
          "\n ┄┄┄┄┄┄"+
         "\n复制这条消息, 打开【手机淘宝】即可购买つ" + dd.kl + " " + dd.Url + "" +
         "\n（优惠汇总）点此进入要什么就搜索什么：http://" + dd.hz + "</textarea>");
            html += ("</div><div class=\"pop_detail clearfix\"><p></p></div></div>");
            $("#pop").html(html);
            $("#mask").show();
            $("#pop").show();
        }
    });
          
         

}
function zhuanfa(auctionId) {
    $.ajax({
        url: '/agentcenter/ApplyZhuanfa',
        type: 'post',
        data: {
            auctionId: auctionId
        },
        success: function (data) {
            if (data.State) {

                alert("转发成功")

                $('#zhuanfa').hide();
            }
        }
    });

}

function copy(obj) {
    $(obj).addClass("btn-success");
    $(obj).addClass("btn-success");
    $(obj).html("已复制");
    var clipboard = new Clipboard('#copyid', {
        text: function (trigger) {
            return $(".taodetail").val();
        }
    });
    setTimeout(function () {
        $(obj).removeClass("btn-success");
        $(obj).html("复制文案");
    }, 1500);
} 
function checkForm(obj) {
    var val = $.trim($('.search-ipt').val()), err = '';
    if (/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@@.\w_]*)#?(?:[\w]*))?)$/g.test(val)) {
        err = '粘贴商品标题试试~（链接地址不支持）！';
    }
    if (val == '') {
        err = '搜索内容不能为空！';
    }
    if (err) {
        layer.msg(err);

    }
    return !err;
}
       
var limit = 1;
function get_list(ty) {
    if (ty == 1) {
        $(".results .allitem-panel").html('');
    }

    $.ajax({
        type: "post",
        url: "/AgentCenter/GetSearchProduct",
        data: {
            limit: limit,
            keyword: $("#keyword").val()
        },
        dataType: "json",
        success: function (data) {
            if (data.status == 1) {
                var list = data.DataList;
                var content = '';
                listOBJ = list;
                for (var i = 0; i < list.length; i++) {
                    content += '<li><div class="result-img">';
                         
                    content += "<a onclick=\"ProductPop('" +escape( JSON.stringify(list[i]).replace(/\"/g, "'")) + "')\" >";
                    content += '<img src="' + list[i]['pic_url'] + '_200x200q60.jpg" alt=""></a></div><div class="result-desc">';
                    content += '<h3 class="f14">' + list[i]['title'] + '</h3>';
                    content += '<p class="f13">原价 ¥ <del>' + list[i]['org_price'] + '</del> 已售' + list[i]['goods_sale'] + '件</p>';
                    content += '<p class="f13">券后价 ¥ <strong class="f16">' + list[i]['price'] + '</strong></p>';
                    if (list[i]['coupons_price']) {
                        content += '<p class="f13"><span>券<i>' + list[i]['coupons_price'] + '元</i></span> 剩余' + list[i]['couponLeftCount'] + '张</p>';
                    }
                    content += "<a onclick=\"ProductPop('" + escape(JSON.stringify(list[i]).replace(/\"/g, "'")) + "')\"><button type=\"button\" class=\"f13 sstop\" >我要推广</button></a></div></li>";
                 
                }
                $(".results .allitem-panel").append(content);
                if (list.length > 1) {
                    $('#sobot').show();
                    $('#sobot_index').hide();
                } else {
                    $('#sobot').hide();
                    $('#sobot_index').show();
                }
                limit++;

            } else if (data.status == 2) {
                $('#sobot').hide();
                $('#sobot_index').show();

            } else {
                $('#sobot').hide();
                $('#sobot_index').show();
            }
            if (data.status == 2 && limit == 1) {
                layer.open({
                    content: '对不起，没有找到任何优惠！'
                 , btn: ['继续查看']
                 , shadeClose: true
                 , no: function () {
                     window.location.href = '/Search.html';
                 }
                });
            }
        },
    });


}
    

    




function getScrollTop() {
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}
function getClientHeight() {
    var clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
    }
    else {
        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
    }
    return clientHeight;
}

function getScrollHeight() {
    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}


$(window).scroll(function () {
    if (getScrollTop() + getClientHeight() == getScrollHeight()) {
        get_list(0);
    }
});


document.getElementById('wrap').style.display = 'block';
